package com.masterclient.app.modules.income.contract;

import android.support.annotation.NonNull;

import com.eoner.baselibrary.utils.EncryptUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.common.config.Config;
import com.masterclient.app.base.ProgressLoginDialog;
import com.masterclient.app.businessmodule.usermodule.login.beans.CaptchaBean;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.income.dealer.model.DealerBankCardBean;
import com.masterclient.app.modules.income.model.ApplyWithdrawBean;
import com.masterclient.app.modules.income.model.WithdrawChargeBean;
import com.masterclient.app.utils.CaptchaUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/08/09
*/
public class ApplyWithdrawPresenter extends ApplyWithdrawContract.Presenter{

    @Override
    public void getWithdrawPreviewData() {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().getWithdrawPreviewData(), (Consumer<CommonBaseResponse<ApplyWithdrawBean>>) bean -> {
            if(bean.getCode() == 0){
                mView.showData(bean.getData());
            }else {
                mView.showDataFailed(bean.getMsg());
            }
        },new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                mView.showDataFailed("网络异常，请重试");
                super.accept(o);
            }
        }));
    }

    @Override
    public void applyWithdraw(String bankcardCertId,String amount,String mobile,String captcha, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("正在加载");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bankcard_cert_id",bankcardCertId);
            jsonObject.put("amount",amount);
            jsonObject.put("mobile",mobile);
            jsonObject.put("captcha",captcha);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseResponse<ApplyWithdrawBean>> observable = RetrofitUtil2.getInstance().applyWithdraw(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse<ApplyWithdrawBean>>) bean -> {
            if(bean.getCode() == 0){
                progressLoginDialog.dismiss();
                mView.applyWithdrawSuccess(bean.getData());
            }else {
                progressLoginDialog.dismiss();
                mView.showDataFailed(bean.getMsg());
            }
        },(Consumer<Throwable>) o -> {
            o.printStackTrace();
            progressLoginDialog.dismiss();
            mView.showDataFailed("网络异常，请重试");
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getBankCardInfo(ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("正在加载");
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().getBankCardList(), (Consumer<CommonBaseResponse<List<DealerBankCardBean>>>) bean -> {
            if (bean.getCode() == 0){
                progressLoginDialog.dismiss();
                mView.getBankSuccess(bean.getData());
            }else {
                progressLoginDialog.dismiss();
                mView.getBankCardFail(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            progressLoginDialog.dismiss();
            mView.getBankCardFail("网络异常，请重试");
        }));
    }

    @Override
    public void withdrawCharge(String amount, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("正在加载");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("amount",amount);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseResponse<WithdrawChargeBean>> observable = RetrofitUtil2.getInstance().withdrawCharge(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse<WithdrawChargeBean>>) bean -> {
            if(bean.getCode() == 0){
                progressLoginDialog.dismiss();
                mView.withdrawChargeSuccess(bean.getData());
            }else {
                progressLoginDialog.dismiss();
                mView.withdrawChargeFail(bean.getMsg());
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@io.reactivex.annotations.NonNull Throwable o) throws Exception {
                super.accept(o);
                progressLoginDialog.dismiss();
                mView.withdrawChargeFail("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getCaptcha(@NonNull String phoneNum, @NonNull String type, String ne, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("正在加载");
        String captchaValidate = "";
        if (CaptchaUtil.CAPTCHA_NUMBER_TIMES >= Config.CAPTCHA_MAX_TIMES){
            Map<String,String> map = new HashMap<>();
            map.put("failureNum","3");
            map.put("clientTime",System.currentTimeMillis()/1000+"");
            captchaValidate = EncryptUtil.encryptRSA(new Gson().toJson(map),Config.captchaKey);
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile",phoneNum);
            jsonObject.put("type",type);
            jsonObject.put("NECaptchaValidate",ne);
            jsonObject.put("captchaValidate",captchaValidate);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CaptchaBean> observable = RetrofitUtil2.getInstance().getCaptcha(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CaptchaBean>) bean -> {
            if("0".equals(bean.getCode())){
                progressLoginDialog.dismiss();
                mView.getCaptchaResult("" + true);
            }else {
                progressLoginDialog.dismiss();
                mView.showError(bean.getMsg());
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@io.reactivex.annotations.NonNull Throwable o) throws Exception {
                super.accept(o);
                progressLoginDialog.dismiss();
                mView.showError("获取验证码失败");
            }
        });
        mRxManager.add(disposable);
    }
}